<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Area;
use app\common\model\ProductCate;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;

class Business extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Business';

    protected $hasOne = ['user'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'user_code', 'title' => '会员', 'templet' => '<div>{{d.user.account}}</div>'])
            ->addRow(['field' => 'type', 'title' => '店铺类型', 'templet' => '<div>{{d.type == 1?"线上":"线下"}}</div>'])
            ->addRow(['field' => 'title', 'title' => '店铺名称', 'width' => 200])
            ->addRow(['field' => 'contacts', 'title' => '联系人'])
            ->addRow(['field' => 'mobile', 'title' => '手机号', 'width' => 110])
            ->addRow(['field' => 'customer_tel', 'title' => '客服电话', 'width' => 110])
            ->addRow(['field' => 'logo', 'title' => '店铺logo', 'templet' => '#logoTpl'])
            ->addRow(['field' => 'license', 'title' => '营业执照', 'templet' => '#licenseTpl'])
            ->addRow(['field' => 'full_address', 'title' => '店铺地址'])
            ->addRow(['field' => 'create_time', 'title' => '入驻时间', 'width' => 150])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 80]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['title' => '编辑', 'width' => 1920, 'height' => 1080]])
            ->addTopButtons(['deleteAll'])
            ->setSearch($this->setSearch())
            ->addExtraHtml(['BusinessApply'])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/用户名/手机号', 'input', '', '\app\common\model\User', 'code|account|mobile,id'])
            ->setSearch(['contacts__mobile__customer_tel', '联系人/手机号/客服电话', 'input', '', '', '', 'like'])
            ->setSearch(['title', '店铺名称', 'input', '', '', '', 'like'])
            ->setSearch(['status', '状态', 'select', [1 => '待审核', 2 => '已审核', 3 => '已驳回']]);

    }

    public function edit($name = '', $id = '', $tpl = 'business/edit', $info = '')
    {
        $info = $this->model::find($id);
        $info['license'] = $info['license'] ? explode(',', $info['license']) : '';
        $info['cate_1'] = $info['cate_1'] ? explode(',', $info['cate_1']) : '';
        View::assign('bank', \app\common\model\Bank::select());
        View::assign('cate', ProductCate::getCate());
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['license'] = isset($param['license']) ? implode(',', $param['license']) : [];
        $param['cate_1'] = isset($param['cate_1']) ? implode(',', $param['cate_1']) : [];
        if(isset($param['type']) && $param['type'] == 2){
            $address = Area::where('code', $param['district_id'])->find();
            if (empty($address)) {
                return json(['code' => -1, 'msg' => '地址错误！']);
            }
            $param['full_address'] = $address->province . $address->city . $address->district . $param['address_details'];
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
